//
//  YMBaseViewController.m
//  PCR
//
//  Created by shumei on 16/6/3.
//  Copyright © 2016年 shumei. All rights reserved.
//

#import "YMBaseViewController.h"

@interface YMBaseViewController ()

@end

@implementation YMBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) setTopTitleWithTitle:(NSString *)title
{
    CGSize titleSize              = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 44)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Nav_Top_Title_Font]}
                                                        context:nil].size;
    UILabel *titleLable           = [[UILabel alloc]initWithFrame:(CGRect){zero,zero,titleSize.width,44}];
    titleLable.font               = [UIFont fontWithName:@"Helvetica-Bold" size:Nav_Top_Title_Font];//加粗
    titleLable.textColor          = [UIColor blackColor];
    titleLable.text               = title;
    titleLable.textAlignment      = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLable;
}

-(void)setTopTitleWithTitle:(NSString *)title
                    OrImage:(NSString *)imageName
{
    CGSize titleSize              = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 44)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Nav_Top_Title_Font]}
                                                        context:nil].size;
    UIImage * image               = [UIImage imageNamed:imageName];
    CGFloat imageWidth            = image.size.width;
    UIButton * button             = [UIButton buttonWithType:0];
    button.frame                  = (CGRect){zero,zero,titleSize.width + imageWidth ,44};
    [button setTitle:title forState:0];
    button.titleLabel.font        = [UIFont systemFontOfSize:Nav_Top_Title_Font];
    [button setTitleColor:[UIColor blackColor] forState:0];
    [button setImage:image forState:0];
    button.imageEdgeInsets        = UIEdgeInsetsMake(0, titleSize.width, 0, 0);
    button.titleEdgeInsets        = UIEdgeInsetsMake(0, - imageWidth - 10, 0, 0);
    [button addTarget:self
               action:@selector(topBtnAction:)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = button;
    
}

- (void)setLeftItemWithTitle:(NSString *)title
                     OrImage:(NSString *)imageName
{
    UIButton *bt                          = [[UIButton alloc]init];
    if (title)
    {
        CGSize size               = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 44)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Nav_Left_Title_Font]}
                                                        context:nil].size;
        bt.frame                  = CGRectMake(zero, zero, size.width + marg, 44);
        [bt.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:Nav_Left_Title_Font]];//加粗
        bt.titleEdgeInsets        = UIEdgeInsetsMake(0, -15, 0, 0);
        [bt setTitle:title forState:UIControlStateNormal];
        bt.userInteractionEnabled = NO;
        [bt setTitleColor:[UIColor blackColor]
                 forState:UIControlStateNormal];
        [bt setTitleColor:[UIColor lightGrayColor]
                 forState:UIControlStateHighlighted];
    }
    else if (imageName)
    {
        CGSize size               = [UIImage imageNamed:imageName].size;
        bt.frame                  = CGRectMake(zero, zero, size.width + marg, 44);
        bt.imageEdgeInsets        = UIEdgeInsetsMake(0, -size.width, 0, 0);
        [bt setImage:[UIImage imageNamed:imageName]
            forState:UIControlStateNormal];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:bt];
    [bt addTarget:self
           action:@selector(leftBtAction:)
 forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)setRightItemsWithTitles:(NSArray *)titles
                       OrImages:(NSArray *)images
{
    NSMutableArray *items                   = [NSMutableArray array];
    int i                                   = 0;
    if (titles) {
        for (NSString *title in titles) {
            
            CGSize size        = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 44)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Nav_Right_Title_Font]}
                                                     context:nil].size;
            UIButton *bt       = [[UIButton alloc]initWithFrame:(CGRect){zero,zero,size.width + 10,44}];
            [bt setTitle:title forState:UIControlStateNormal];
            [bt setTitleColor:[UIColor blackColor]
                     forState:UIControlStateNormal];
            bt.titleLabel.font = kFont(Nav_Right_Title_Font);
            bt.tag             = comTag + i;
            [bt addTarget:self
                   action:@selector(rightAction:)
         forControlEvents:UIControlEventTouchUpInside];
            [items addObject:[[UIBarButtonItem alloc]initWithCustomView:bt]];
            i++;
        }


    }else if (images)
    {
        for (NSString *name in images) {

            CGSize size        = [UIImage imageNamed:name].size;

            UIButton *bt       = [[UIButton alloc]initWithFrame:(CGRect){zero,zero,size.width + 10,44}];
            [bt setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
            [bt addTarget:self
                   action:@selector(rightAction:)
         forControlEvents:UIControlEventTouchUpInside];

            [items addObject:[[UIBarButtonItem alloc]initWithCustomView:bt]];
            bt.tag             = comTag + i;
            i ++;
        }
        
    }
    self.navigationItem.rightBarButtonItems = items;
    
    
}

- (void)leftBtAction:(UIButton *)sender
{
    [YMBaseTabBarViewController shareInstance].scroll_View.scrollEnabled = YES;
    if (self.LeftButtonAction)
    {
        self.LeftButtonAction();
    }
    [self.navigationController  popViewControllerAnimated:YES];
    
}


-(void)topBtnAction:(UIButton *)sender
{
    
    if (_topButtonAction)
    {
        self.topButtonAction(sender);
    }
    sender.selected = !sender.selected;
}

- (void)rightAction:(UIButton *)sender
{
    
    if (self.RightButtonAction)
    {
        self.RightButtonAction(sender);
        
    }
    
}

-(void)actionWithBlocksLeftButtonHandler:(void (^)(void))LeftHandler
{
    self.LeftButtonAction  = LeftHandler;
}

-(void)actionWithBlocksTopButtonHandler:(void (^)(UIButton *sendBtn))TopHandler
{
    self.topButtonAction   = TopHandler;
}


-(void)actionWithBlocksRightButtonHandler:(void (^)(UIButton *))RightHandler
{
    self.RightButtonAction = RightHandler;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
